package inits

import (
	"github.com/google/uuid"
	capi "github.com/hashicorp/consul/api"
)

type ConsulClient struct {
	client *capi.Client
}

func InitConsul(addr string) *ConsulClient {
	data := capi.DefaultConfig()
	data.Address = addr
	client, err := capi.NewClient(data)
	if err != nil {
		panic(err)
	}
	return &ConsulClient{client: client}
}

type GrpcConf struct {
	Name    string
	Tags    []string
	Port    int
	Address string
}

func (c *ConsulClient) RegisterService(cfg GrpcConf) error {
	config := capi.AgentServiceRegistration{
		ID:      uuid.NewString(),
		Name:    cfg.Name,
		Tags:    cfg.Tags,
		Port:    cfg.Port,
		Address: cfg.Address,
	}
	return c.client.Agent().ServiceRegister(&config)
}
